package com.sxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.common.DataGridView;
import com.sxt.mapper.BillsMapper;
import com.sxt.service.BillTypeService;
import com.sxt.service.BillsService;
import com.sxt.vo.BillsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BillsServiceImpl extends ServiceImpl<BillsMapper, Bills> implements BillsService{

    @Autowired
    private BillsMapper billsMapper;

    @Autowired
    private BillTypeService billTypeService;

    @Override
    public DataGridView queryAllBills(BillsVo billsVo) {
        QueryWrapper<Bills> qw=new QueryWrapper<>();
        qw.eq(billsVo.getTypeid()!=null,"typeid",billsVo.getTypeid());
        qw.ge(billsVo.getStartTime()!=null,"billtime",billsVo.getStartTime());
        qw.le(billsVo.getEndTime()!=null,"billtime",billsVo.getEndTime());
        qw.orderByDesc("billtime");

        IPage<Bills> page=new Page<>(billsVo.getPage(),billsVo.getLimit());

        this.billsMapper.selectPage(page,qw);
        List<Bills> records = page.getRecords();
        for (Bills record : records) {
            if(record.getTypeid()!=null){
                BillType billType = billTypeService.getById(record.getTypeid());
                record.setTypename(billType.getName());
            }
        }
        return new DataGridView(page.getTotal(), records);
    }
}
